package com.lixh.sdk.base.event

import android.app.Presentation
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.LifecycleOwner
import com.lixh.sdk.base.BuildConfig
import com.lixh.sdk.base.util.log

object EventSend {
    val obs: MutableMap<LifecycleOwner, Map<String, EventRegister>> = HashMap()


    //通知 key 注册的 和参数 k to v
    @Synchronized
    fun post(key: String, param: Bundle? = bundleOf()) {
        // 是否注册有 key
        try {
            obs.keys.filter {
                !obs[it].isNullOrEmpty()
            }.forEach { view ->
                obs[view]?.apply {
                    this[key].apply {
                        this?.callback(param)
                    }
                }
            }
        } catch (e: Exception) {
            if (BuildConfig.DEBUG) {
                e.message?.log("EventSend")
            }
        }
    }

}


object EventPresentSend {
    val obs: MutableMap<Presentation, Map<String, EventRegister>> = HashMap()


    //通知 key 注册的 和参数 k to v
    @Synchronized
    fun post(key: String, param: Bundle = bundleOf()) {
        // 是否注册有 key
        try {
            obs.keys.filter {
                !obs[it].isNullOrEmpty()
            }.forEach { view ->
                obs[view]?.apply {
                    this[key].apply {
                        this?.callback(param)
                    }
                }
            }
        } catch (e: Exception) {
            if (BuildConfig.DEBUG) {
                e.message?.log("EventPresentSend")
            }
        }
    }

}